package com.cqrt.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.mapper.RTTaskMapper;
import com.cqrt.service.RTTaskService;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.rpm.model.dto.job.TyppmJobDTO;
import com.hustcad.plm.rpm.serviceextend.impl.QueryJobListExtendServiceImpl;
import com.ty.ppm.api.service.job.JobService;
import com.ty.ppm.model.dto.job.JobDTO;
import com.ty.ppm.model.utils.Tools;
import com.ty.ppm.service.mapper.job.JobMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Primary
public class RTQueryJobListExtendServiceImpl extends QueryJobListExtendServiceImpl {
    @Resource
    private JobService jobService;
    @Resource
    private RTTaskMapper taskMapper;

    @Override
    public PageInfo<TyppmJobDTO> queryJobPageByParamsChildMarkAfter(PageInfo<TyppmJobDTO> pageInfo) {
        List<TyppmJobDTO> list = pageInfo.getList();
        List<String> jobOidList = new ArrayList<>();
        for (TyppmJobDTO typpmJobDTO : list) {
            if(StringUtils.isEmpty(typpmJobDTO.getJobNamePath()) || StringUtils.isBlank(typpmJobDTO.getJobNamePath())){
                jobOidList.add(typpmJobDTO.getOid());
            }
        }
        if(CollUtil.isNotEmpty(jobOidList)){
            List<JSONObject> data = this.taskMapper.queryParentJobOid(jobOidList);
            Map<String, String> map = data.stream()
                    .collect(Collectors.toMap(
                            jsonObject -> jsonObject.getString("JOBID"),
                            jsonObject -> jsonObject.getString("PARENTJOBOID"),
                            (existing, replacement) -> replacement,
                            HashMap::new
                    ));
            List<String> parentJobOidList = ListUtil.toList(map.values());
            if (CollUtil.isNotEmpty(parentJobOidList)) {
                List<JSONObject> objectList = taskMapper.queryJobNameByJobOid(parentJobOidList);
                Map<String, String> jobNameMap = objectList.stream()
                        .collect(Collectors.toMap(
                                jsonObject -> jsonObject.getString("OID"),
                                jsonObject -> jsonObject.getString("NAME"),
                                (existing, replacement) -> replacement,
                                HashMap::new
                        ));
                Map<String, String> jobNamePathMap = this.jobService.queryJobNamePath(parentJobOidList);

                for (TyppmJobDTO job : list) {
                    if(map.containsKey(job.getOid())){
                        String parentJobId = map.get(job.getOid());
                        String path = jobNamePathMap.get(parentJobId)+jobNameMap.get(parentJobId);
                        job.setJobNamePath(path);
                    }
                    if(StringUtils.isBlank(job.getJobNamePath()) || StringUtils.isEmpty(job.getJobNamePath())){
                        job.setJobNamePath(job.getProjectName()+"/");
                    }
                }
            }
        }
        return pageInfo;
    }
}
